package com.cx.base.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.cx.base.R;
import com.cx.base.presenter.IPresenter;
import com.cx.base.view.BaseView;
import com.cx.base.view.IView;
import com.cx.base.view.load.LoadingController;
import com.cx.base.view.load.LoadingDialog;
import com.cx.util.device.FragmentHelper;
import com.cx.util.device.ToastHelper;

import dagger.android.support.DaggerDialogFragment;
import io.reactivex.disposables.CompositeDisposable;


public abstract class IFragment extends DaggerDialogFragment
        implements IView {

    private DialogFragment mProgress;
    private BaseView mBaseView;

    private int res;


    @Override
    public void finish() {
        FragmentActivity activity = getActivity();
        if (activity != null)
            activity.finish();
    }


    public void setRes(int res) {
        this.res = res;
    }


    public int getRes() {
        return res;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBaseView = new BaseView(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onStart() {
        super.onStart();
        mBaseView.onStart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBaseView.onDestroy();
        mBaseView = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onLoadFinish();
        mProgress = null;
    }

    @Override
    public void addPresenter(IPresenter.Presenter<? extends IView> presenter) {
        mBaseView.addPresenter(presenter);
    }

    @Override
    public LoadingController getLoadingController() {
        return mBaseView.getLoadingController();
    }

    @Override
    public CompositeDisposable getDisposable() {
        return mBaseView.getDisposable();
    }

    /* ************************same*****************************/


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mBaseView.onActivityResult(requestCode, resultCode, data);
    }

    protected void setLoadingDialog(DialogFragment dialog) {
        mProgress = dialog;
    }


    @Override
    public void showDialog(DialogFragment fragment) {
        FragmentHelper.showDialog(fragment, getChildFragmentManager());
    }


    protected void replaceFragment(IFragment fragment,
                                   FragmentManager manager,
                                   Pair<View, String>... sharedElements) {
        replaceFragment(fragment,
                manager,
                false,
                sharedElements);
    }

    protected void replaceFragment(IFragment fragment,
                                   FragmentManager manager,
                                   boolean addToBackStack,
                                   Pair<View, String>... sharedElements) {
        FragmentHelper.replace(fragment.getRes(),
                fragment,
                manager,
                addToBackStack,
                sharedElements);
    }


    @Override
    public Bundle getTransitionBundle() {
        return ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity()).toBundle();
    }


    @Override
    public void showToast(String msg) {
        ToastHelper.showMsg(msg);
    }

    @Override
    public void showToast(int id) {
        showToast(getString(id));
    }


    @Override
    public void onLoadStart() {
        if (mProgress == null)
            mProgress = getLoadingDialog();
        showDialog(mProgress);
    }

    protected DialogFragment getLoadingDialog() {
        LoadingDialog progressDialog = new LoadingDialog();
        progressDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Dialog);
        progressDialog.setCancelable(false);
        return progressDialog;
    }

    @Override
    public void onLoadFinish() {
        if (mProgress != null)
            mProgress.dismissAllowingStateLoss();
    }
}
